﻿using Module.Common.PubEvent;
using Prism.Events;
using Prism.Mvvm;
using System.Windows;

namespace Module.Common.ViewModels
{
    public class ChessBoardViewModel : BindableBase
    {
        private readonly IEventAggregator Ea;
        private readonly string ClassName; // 用于记录消息发出的位置

        private bool _fanZhuanQipan;
        public bool FanZhuanQipan   // 棋盘翻转状态
        {
            get { return _fanZhuanQipan; }
            set
            {
                SetProperty(ref _fanZhuanQipan, value);
                if (_fanZhuanQipan) { RedUp = Visibility.Visible; BlackUp = Visibility.Hidden; }
                else { RedUp = Visibility.Hidden; BlackUp = Visibility.Visible; }

                // 此处棋盘更换处理代码
            }
        }

        private Visibility _redUp; // 红方在上
        public Visibility RedUp { get { return _redUp; } set { SetProperty(ref _redUp, value); } }

        private Visibility _blackUp; // 黑方在上
        public Visibility BlackUp { get { return _blackUp; } set { SetProperty(ref _blackUp, value); } }

        private bool _hasNewText; // 走棋结果提示内容改变时，使用淡入淡出动画。
        public bool HasNewText { get { return _hasNewText; } set { SetProperty(ref _hasNewText, value); } }

        private string messagetext; // // 走棋结果提示信息，如走棋违规、将军、绝杀等。显示位置在棋盘正中间。
        public string Messagetext
        {
            get { return messagetext; }
            set
            {
                HasNewText = false;
                if (value.Length > 0) HasNewText = true;
                SetProperty(ref messagetext, value);
            }
        }

        /// <summary>
        /// 棋盘类的ViewModel
        /// </summary>
        /// <param name="ea"></param>
        public ChessBoardViewModel(IEventAggregator ea)
        {
            this.Ea = ea;
            ClassName = this.GetType().Name;
            FanZhuanQipan = false; // 初始为黑方在上
            
            Ea.GetEvent<SystemMessageEvent>().Subscribe(SystemMessageProcess);
            Ea.GetEvent<ServiceMessageEvent>().Subscribe(ServiceMessageProcess);
            Ea.GetEvent<ModuleMessageEvent>().Subscribe(ModuleMessageProcess);

        }
        #region 订阅消息处理
        private void ServiceMessageProcess(ServiceMessage obj)
        {
            switch (obj.MessageName)
            {
                case "qiziGetClicked": Messagetext = ""; ReplyMessage(obj.MessageName); break;
                case "hasJiangJun":
                    if (obj.jiangJun_Message[0] == 0) Messagetext = "黑方正被将军";
                    if (obj.jiangJun_Message[0] == 16) Messagetext = "红方正被将军";
                    ReplyMessage(obj.MessageName);
                    break;
                case "hasJueSha":
                    if (obj.Id == 0) Messagetext = "黑方已被绝杀，本局结束";
                    if (obj.Id == 16) Messagetext = "红方已被绝杀，本局结束";
                    ReplyMessage(obj.MessageName);
                    break;
                case "hasKunBi":
                    if (obj.Id == 0) Messagetext = "黑方已被困毙，本局结束";
                    if (obj.Id == 16) Messagetext = "红方已被困毙，本局结束";
                    ReplyMessage(obj.MessageName);
                    break;
                case "noJiangjunORJueshaORKunbi":
                    break;
                default:
                    break;
            }
        }

        private void SystemMessageProcess(SystemMessage obj)
        {
            switch (obj.MessageName)
            {
                case "qipanInverse":    // 棋盘翻转消息
                    FanZhuanQipan = !FanZhuanQipan; ReplyMessage(obj.MessageName);
                    break;
                default:
                    break;
            }
        }
        private void ModuleMessageProcess(ModuleMessage obj)
        {
            switch (obj.MessageName)
            {
                case "qiziGetClicked": Messagetext = ""; break;
                case "moveSideError":
                    Messagetext = obj.Content;
                    ReplyMessage(obj.MessageName);
                    break;
                default:
                    break;
            }
        }
        private void ReplyMessage(string messagetext)
        {
            Ea.GetEvent<ReplyMessageEvent>().Publish(new ReplyMessage()
            {
                MessageName = "",
                SenderFrom = ClassName,
                Content = $"【{ClassName}】：已收到并处理了订阅消息【{messagetext}】",
                ModuleName = this.GetType().Module.Name,
            });
        }
        #endregion
    }
}
